"""
red:警报         31m
green:系统提示    32m
yellow:遍历展示   33m
blue:选项        34m
青:输入           96m
"""
from endOfPeriodProject.teacher.teacher import *


class TeacherManger(object):
    def __init__(self):
        self.t_list = []

    def run(self):
        self.load()
        while True:
            try:
                self.show_question()
                answer = int(input("\033[96m Please enter the number what you want to use: \033[0m"))
                if answer == 1:
                    # Query all teachers
                    self.query_all_teachers()
                elif answer == 2:
                    # Query one teachers
                    self.query_one_teacher()
                elif answer == 3:
                    # Insert a new teachers
                    self.insert_teacher()
                elif answer == 4:
                    # Remove a teachers
                    self.remove_teacher()
                elif answer == 5:
                    # Update a teachers's information
                    self.update_teacher()
                elif answer == 6:
                    # Save teachers information into teachers data
                    self.save_to_data()
                elif answer == 7:
                    # Edit
                    print("\033[47m ------Thank you for your ues!!!------ \033[0m")
                    break
                elif answer == '' or answer is None:
                    print("\033[31m Unknown answer, please enter again\033[0m")
                    self.show_question()
                    self.run()
                else:
                    print("\033[31m Unknown answer, please enter again\033[0m")
                    self.show_question()
                    self.run()
            except ValueError:
                print("\033[31m Unknown answer, please enter again\033[0m")
                self.show_question()
                self.run()

    def show_question(self):
        print("\033[34m 1.Query all teachers\033[0m\n"
              "\033[34m 2.Query one teachers\033[0m\n"
              "\033[34m 3.Insert a new teachers\033[0m\n"
              "\033[34m 4.Remove a teachers\033[0m\n"
              "\033[34m 5.Update a teachers's information\033[0m\n"
              "\033[34m 6.Save teachers information into teachers data\033[0m\n"
              "\033[34m 7.Edit\033[0m")

    def query_all_teachers(self):
        if len(self.t_list) == 0:
            print("\033[31m !!!List is null, no data here!!! \033[0m")
        else:
            for i in self.t_list:
                print(f"\033[33m{i}\033[0m")

    def query_one_teacher(self):
        if len(self.t_list) == 0:
            print("\033[31m !!!List is null, no data!!! \033[0m")
        else:
            this_teacher = input("\033[96m Please enter that teachers's name what you want query: \033[0m")
            for i in self.t_list:
                if i.name == this_teacher:
                    print(f"\033[33m {i} \033[0m")
                else:
                    print("\033[31m !!!Not find this teachers!!! \033[0m")

    def insert_teacher(self):
        name = input("\033[96m ---Please enter your name: \033[0m")
        age = input("\033[96m ---Please enter your age: \033[0m")
        gender = input("\033[96m ---Please enter your gender: \033[0m")
        id = input("\033[96m ---Please enter your id: \033[0m")
        tel = input("\033[96m ---Please enter your telephone number: \033[0m")
        course = input("\033[96m ---Please enter your teach course: \033[0m")
        new_t = Teacher(name, age, gender, id, tel, course)
        self.t_list.append(new_t)
        print("\033[32m This is the information of this teachers's\033[0m ")
        self.query_all_teachers()

    def remove_teacher(self):
        counter = 0
        if len(self.t_list) == 0:
            print("\033[31m !!!List is null, no data here!!! \033[0m")
        else:
            remove_t = input("\033[96m Please enter the name of teachers what you want to remove: \033[0m")
            for i in self.t_list:
                counter += 1
                if i.name == remove_t:
                    counter -= 1
                    self.t_list.remove(i)
                    if counter != self.t_list.__len__():
                        print("\033[31m !!!Not found this teachers!!! \033[0m")
            print("\033[32m List after removed: \033[0m")
            self.query_all_teachers()

    def update_teacher(self):
        counter = 0
        if len(self.t_list) == 0:
            print("\033[31m !!!List is null, no data!!! \033[0m")
        else:
            update_teacher = input("\033[96m Please enter the teachers's name what you want to update that: \033[0m")
            for i in self.t_list:
                counter += 1
                if i.name == update_teacher:
                    counter -= 1
                    i.name = input("\033[96m ---Please enter the changed teachers's name: \033[0m")
                    i.age = input("\033[96m ---Please enter the changed teachers's age: \033[0m")
                    i.gender = input("\033[96m ---Please enter the changed teachers's gender: \033[0m")
                    i.id = input("\033[96m ---Please enter the changed teachers's id: \033[0m")
                    i.tel = input("\033[96m ---Please enter the changed teachers's telephone number: \033[0m")
                    i.course = input("\033[96m ---Please enter the changed teachers's course: \033[0m")
                    print(f"\033[33m {i} \033[0m")
                    if counter == self.t_list.__len__():
                        print("\033[31m !!!Not find this teachers!!! \033[0m")

    def save_to_data(self):
        f = open("teacher.data", "w", encoding="utf-8")
        new_list = [i.__dict__ for i in self.t_list]
        a = str(new_list)
        f.write(a)
        f.close()

    def load(self):
        try:
            f = open("../teacher/teacher.data", "r", encoding="utf-8")
        except:
            f = open("../teacher/teacher.data", "w", encoding="utf-8")
        else:
            data = f.read()
            new_list = eval(data)
            self.t_list = [Teacher(i["name"], i["age"], i["gender"], i["id"], i["tel"], i["course"]) for i in new_list]
            f.close()